package com.bilgeadam.shopping.servlet;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Bora SAYINER
 * @since Jan 3, 2015 5:24:15 PM
 * @version 1.0
 */
public class GenericHttpServlet extends HttpServlet {

	protected Cookie getCookie(HttpServletRequest req, String name) {
		Cookie cookie = null;
		Cookie[] cookies = req.getCookies();
		for (Cookie current : cookies) {
			if (current.getName().equals(name)) {
				cookie = current;
				break;
			}
		}
		return cookie;
	}

	protected boolean deleteCookie(HttpServletRequest req, HttpServletResponse resp, String name) {
		boolean result = false;
		Cookie cookie = getCookie(req, name);
		if (cookie != null) {
			cookie.setMaxAge(0);
			resp.addCookie(cookie);
			result = true;
		}
		return result;
	}
}
